package org.jeecg.modules.zhouAdmin.admin.goodsCenter.util;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.aliyun.oss.OSSClient;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Hashtable;

@Component
public class ZXingutil {

    private final static String ENDPOINT = "http://oss-cn-shanghai.aliyuncs.com";
    private final static String ACCESSKEYID = "LTAI4GEZ1VRo4geGJMJETXfM";
    private final static String ACCESSKEYSECRET = "aazkmD8DoXmV9qgT7o6BL8ZaMMj3W1";
    private final static String BUCKETNAME = "mlcc-public-001";

    /**
     * 生成二维码2
     *
     * @param content 内容
     * @return 二维码图片网络地址
     */
    public String encodeImg2(String content) throws Exception {
        BufferedImage bufferedImage = QrCodeUtil.generate(content, 200, 200);
        // 创建输出流，用来存储图片
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", out);// format:图片格式
        // 创建输入流
        ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
        // 上传文件流
        String filename = String.valueOf(System.currentTimeMillis());
        ossClient.putObject(BUCKETNAME, filename + ".jpg", inputStream);
        ossClient.shutdown();
        inputStream.close();
        out.close();
        String qrcodepath = "https://mlcc-public-001.oss-cn-shanghai.aliyuncs.com/" + filename + ".jpg";
        return qrcodepath;
    }

    /**
     * 商品编号生成二维码
     *
     * @param content 商品编号
     * @throws Exception
     */
    @Async
    public String encodeImg(String content) throws Exception {
        try {
            int size = 200;
            Hashtable<EncodeHintType, Object> hashtable = new Hashtable<EncodeHintType, Object>();
            // 容错率:L<M<Q<H
            hashtable.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
            // 编码格式
            hashtable.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // 外边距
            hashtable.put(EncodeHintType.MARGIN, 1);

            BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hashtable);

            // BufferedImage:内存中的一张图片-->二维码-->需要一个boolean[][]-->BitMatrix
            BufferedImage bufimg = new BufferedImage(size, size, BufferedImage.TYPE_INT_BGR);
            for (int x = 0; x < size; x++) {
                for (int y = 0; y < size; y++) {
                    // 注意:这里的color包并不是java.awt中的Color
                    bufimg.setRGB(x, y, (bm.get(x, y) ? 0 : 16777215));
                }
            }
            // 创建输出流，用来存储图片
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            ImageIO.write(bufimg, "jpg", out);// format:图片格式

            // 创建输入流
            ByteArrayInputStream stream = new ByteArrayInputStream(out.toByteArray());

            // 创建OSSClient实例
            OSSClient ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
            // 上传文件流
            String filename = String.valueOf(System.currentTimeMillis());
            ossClient.putObject(BUCKETNAME, filename + ".jpg", stream);
            ossClient.shutdown();
            stream.close();
            out.close();
            String qrcodepath = "https://mlcc-public-001.oss-cn-shanghai.aliyuncs.com/" + filename + ".jpg";
            return qrcodepath;
        } catch (Exception e) {
            return "图片上传失败了...";
        }

    }
}
